#include "mbappwig.h"
#include "ui_mbappwig.h"

MbappWig::MbappWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MbappWig)
{
    ui->setupUi(this);

    dialog = new History(this);
    connect(ui->pushButton_8tem, SIGNAL(clicked()), this,SLOT(showDialog()));
    //     dialog = new History(this);
    connect(ui->pushButton_10_light,SIGNAL(clicked()),this,SLOT(dataDownSlot()));
    ui->pushButton_12_jiashiqi->setDisabled(true);
    connect(ui->pushButton_11_door,SIGNAL(clicked()),this,SLOT(dataDownSlot()));

}

MbappWig::~MbappWig()
{
    delete ui;
}

void MbappWig::showDialog()
{
    connect(this,SIGNAL(dataUpSignal(QByteArray)),this->dialog,SLOT(dataUpSlot(QByteArray)));

    dialog->setWindowTitle("温湿度历史记录");
    dialog->exec(); // Modal dialog, blocks until closed
    // Alternatively, use dialog->show() for non-modal behavior
}

void MbappWig::stmdataUpSlot(QByteArray buf)
{//温度，湿度，空调，加湿器，温度上阈值，温度下阈值，湿度下阈值


    QString jsonString=buf;


    QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8());

    // 检查解析是否成功
    if (!doc.isNull() && doc.isObject()) {
        QJsonObject jsonObj = doc.object();//创建对象

        // 读取JSON对象中的值
        int type = jsonObj["type"].toInt();
        if(type==1)
        {
            bool result = jsonObj["result"].toBool();
            QJsonArray data = jsonObj["data"].toArray();

            //获取数组个数
            int n=data.size();
            qDebug()<<n;

            // 遍历数组并输出每个对象的内容
            for (const QJsonValue &value : data) {
                QJsonObject obj = value.toObject();
                int key = obj["key"].toInt();
                QString val = obj["val"].toString();
                qDebug() << "Key:" << key << ", Val:" << val;

                if(key==301)
                {QString temtext("温度：");
                    temtext.append(val);
                    temtext.append("度");
                    ui->pushButton_8tem->setText(temtext);
                }
                if(key==302)
                {QString temtext("湿度：");
                    temtext.append(val);
                    temtext.append("%");
                    ui->pushButton_9_hum->setText(temtext);
                }
                if(key==303&&air==0)
                {QString temtext("空调：");
                    if(val.toInt()==1)
                    {  temtext.append("开");
                        flag1_kongtiao=1;
                        air=1;
                   }
                    else
                    {temtext.append("关");
                        flag1_kongtiao=0;
                        air=1;
                    }


                    ui->pushButton_10_light->setText(temtext);
                }
                if(key==304)
                {QString temtext("加湿器：");
                    if(val.toInt()==1)
                    {  temtext.append("开");}
                    else
                    {temtext.append("关");}
                    ui->pushButton_12_jiashiqi->setText(temtext);
                }
                if(key==305)
                {QString temtext("温度上阈值(度)：");
                    temtext.append(val);
                    temtext.append("");
                    ui->label_temup->setText(temtext);

                }
                if(key==306)
                {QString temtext("温度下阈值（度）：");
                    temtext.append(val);
                    temtext.append("");
                    ui->label_2_temdown->setText(temtext);

                }
                if(key==307)
                {QString temtext("湿度下阈值(%)：");
                    temtext.append(val);
                    temtext.append("");
                    ui->label_3_humdown->setText(temtext);

                }
                if(key=308&&door==1)
                {QString temtext("门锁：");
                    if(val.toInt()==1)
                    {  temtext.append("开");
                        flag2_door=1;
                        door=0;

                    }
                    else
                    {temtext.append("关");
                        flag2_door=0;
                           door=0;

                    }
                    ui->pushButton_11_door->setText(temtext);
                }
            }

        }
        else if(type==4)
        {
            //需要的时候将信号发送给dialog
            emit dataUpSignal(buf);
        }


    } else {
        qDebug() << "Failed to parse JSON.";
    }
}

void MbappWig::dataDownSlot()
{
    QJsonObject address;
    address["type"] = 2;
    QJsonObject data;
    if(ui->pushButton_10_light== sender())
    {if(flag1_kongtiao==1)
        {
            data["key"]=303;
            data["val"]="0";
          flag1_kongtiao=0;
            QString temtext("空调：");
            temtext.append("关");
            ui->pushButton_10_light->setText(temtext);

        }else if(flag1_kongtiao==0)
        { data["key"]=303;
            data["val"]="1";
            QString temtext("空调：");
            temtext.append("开");
            ui->pushButton_10_light->setText(temtext);
            flag1_kongtiao=1;

        }

    }else if(ui->pushButton_11_door == sender())
    {
        if(flag2_door==1)
                {
                    data["key"]=308;
                    data["val"]="0";
                 flag2_door=0;
                    QString temtext("门锁：");
                    temtext.append("关");
                    ui->pushButton_11_door->setText(temtext);

                }else if(flag2_door==0)
                { data["key"]=308;
                    data["val"]="1";
                    QString temtext("门锁：");
                    temtext.append("开");
                    ui->pushButton_11_door->setText(temtext);
                   flag2_door=1;

                }
    }

 address.insert("data",QJsonValue(data));



    // 将QJsonObject转换为QJsonDocument
    QJsonDocument jsonDoc(address);

    // 输出JSON字符串
    QString jsonString = jsonDoc.toJson(QJsonDocument::Indented);
    qDebug() << "Serialized JSON:" << jsonString;

    // 创建一个QMQTT::Message对象
    quint16 idd = 1;
    QString topic = "/app/data/down";
    QByteArray payload = jsonString.toUtf8();
    //    QByteArray payload = jsonDoc.toBinaryData();
    quint8 qos = 1;
    bool retain = false;
    QMQTT::Message message(idd, topic, payload, qos, retain);
    GlobalVal::mqtt->publish( message);
}
